package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.common.Tools;
import com.yidu.dao.ListenFinanceDetailMapper;
import com.yidu.dao.ListenFinanceMapper;
import com.yidu.dao.ListenPurchaseDetailMapper;
import com.yidu.dao.ListenPurchaseMapper;
import com.yidu.dao.ListenQualityTestingDetailMapper;
import com.yidu.dao.ListenQualityTestingMapper;
import com.yidu.dao.ListenSellDetailMapper;
import com.yidu.dao.ListenSellMapper;
import com.yidu.dao.ListenStockMapper;
import com.yidu.dao.ListenStockRecordMapper;
import com.yidu.domain.ListenBackstageUser;
import com.yidu.domain.ListenFinance;
import com.yidu.domain.ListenFinanceDetail;
import com.yidu.domain.ListenPurchase;
import com.yidu.domain.ListenPurchaseDetail;
import com.yidu.domain.ListenQualityTesting;
import com.yidu.domain.ListenQualityTestingDetail;
import com.yidu.domain.ListenSell;
import com.yidu.domain.ListenSellDetail;
import com.yidu.domain.ListenStock;
import com.yidu.domain.ListenStockRecord;
import com.yidu.service.ListenPurchaseService;

/**
 * @title 采购订单Service实现类 
 * @author 刘武
 * @dateTime 2019年1月14日
 */
@Service
public class ListenPurchaseServiceImpl  implements ListenPurchaseService {
	/**
	 * 注入质检Dao
	 */
	@Resource
	ListenQualityTestingMapper testingDao;
	/**
	 * 注入销售Dao
	 */
	@Resource
	ListenSellMapper sellDao;
	/**
	 * 注入库存Dao
	 */
	@Resource
	ListenStockMapper stockDao;
	/**
	 * 注入库存记录Dao
	 */
	@Resource
	ListenStockRecordMapper stockRecordDao;
	/**
	 * 注入销售详情Dao
	 */
	@Resource
	ListenSellDetailMapper sellDetailDao;
	/**
	 * 注入采购Dao
	 */
	@Resource
	ListenPurchaseMapper purchaseDao;
	/**
	 * 注入财务Dao
	 */
	@Resource
	ListenFinanceMapper financeDao;
	/**
	 * 注入财务记录Dao
	 */
	@Resource
	ListenFinanceDetailMapper financeDetailDao;
	/**
	 * 注入采购详情Dao
	 */
	@Resource
	ListenPurchaseDetailMapper purchaseDetailDao;
	/**
	 * 注入质检详情Dao
	 */
	@Resource
	ListenQualityTestingDetailMapper qualityDetailDao;
	/**
	 * 查询所有
	 * @param purchase 订单对象
	 * @return 订单对象集合
	 */
	@Override
	public List<ListenPurchase> findAll(ListenPurchase purchase) {
		List<ListenPurchase> list=purchaseDao.findAll(purchase);
		//遍历查询出所有
		for (ListenPurchase listenPurchase : list) {
			//转换时间
			listenPurchase.setPurchaseStringTime(Tools.getTimeStr(listenPurchase.getPurchaseTime()));
		}
		//返回集合
		return list;
	}
	/**
	 * 查询行数
	 * @param purchase 订单对象
	 * @return int类型
	 */
	@Override
	public int findRows(ListenPurchase purchase) {
		int rows=purchaseDao.findRows(purchase);
		return rows;
	}
	/**
	 * 删除
	 * @param purchase 订单对象
	 * @return int类型
	 */
	@Override
	public int delete(ListenPurchase purchase) {
		int rows=purchaseDao.delete(purchase);
		return rows;
	}

	/**
	 * @title 采购材料增加和修改
	 * @param ListenPurchase
	 * @return int
	 * @author 江宇
	 * @dateTime 2018年12月7日
	 * @versions 1.0
	 */
	@Override
	public int addUpdateMaterialPurchase(String purchase,String purchaseDateil,String purchaseId,HttpSession session) {
		int rows=0;
		//创建材料采购订单类对象
		ListenPurchase pur=new ListenPurchase();
		//获取当前登陆的Seesion
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		//如果采购订单ID不为空
		if(!Tools.isEmpty(purchaseId)) {
			//创建采购订单Map集合
			Map<String, Object> map=new HashMap<>();
			//采购=订单ID赋值
			map.put("purchaseId", purchaseId);
			//调用查询材料采购订单的方法
			List<ListenPurchaseDetail> list=purchaseDetailDao.materialDetailFindAll(map);
			//循环查询材料采购订单的List集合
			for (ListenPurchaseDetail listenPurchaseDetail : list) {
				//调用删除根据ID删除采购订单的方法
				rows=purchaseDetailDao.deleteByPrimaryKey(listenPurchaseDetail.getPdId());
			}
			
			//拆分前台传来采购明细的数组
			String [] purchaseDateils=purchaseDateil.split("#");
			//循环拆分的数组
			for (int i = 0; i < purchaseDateils.length; i++) {
				//创建材料采购订单明细类对象
				ListenPurchaseDetail pd=new ListenPurchaseDetail();
				//采购订单ID赋值
				pd.setPurchaseId(purchaseId);
				//循环按,号分割后的数组
				for (int j = 0; j < purchaseDateils[i].split(",").length; j++) {
					//UUID主键
					pd.setPdId(Tools.getRandomString(32));
					//业务ID赋值
					pd.setVocationalWorkId(purchaseDateils[i].split(",")[0]);
					//订单材料名称
					pd.setPdName(purchaseDateils[i].split(",")[1]);
					//订单材料单价赋值
					pd.setPdPrice(BigDecimal.valueOf(Double.valueOf(purchaseDateils[i].split(",")[2])));
					//订单材料数量赋值
					pd.setPdNumber(Integer.valueOf(purchaseDateils[i].split(",")[3]));
					//订单材料小计赋值
					pd.setPdSubtotal(BigDecimal.valueOf(Double.valueOf(purchaseDateils[i].split(",")[4])));
					//是否有效(有效)
					pd.setIsva(1);
				}
				//调用增加材料采购订单明细的方法
				rows=purchaseDetailDao.insertSelective(pd);
			}
			//拆分前台传来的采购订单的数组
			String [] purchases=purchase.split(",");
			//采购订单ID
			pur.setPurchaseId(purchaseId);
			//采购数量赋值
			pur.setPurchaseNumber(Integer.valueOf(purchases[0]));
			//采购金额赋值
			pur.setPurchasePrice(BigDecimal.valueOf(Double.valueOf(purchases[1])));
			//供应商赋值
			pur.setSupplierId(purchases[2]);
			//采购说明赋值
			pur.setPurchaseExplain(purchases[3]);
			//审核状态赋值(未审核)
			pur.setAuditingStatus(0);
			//采购订单时间赋值
			pur.setPurchaseTime(new Date());
			//操作人赋值
			pur.setOper(users.getBuName());
			//调用修改采购订单的方法
			rows=purchaseDao.updateByPrimaryKeySelective(pur);
		}else {
			//拆分前台传来的采购订单的数组
			String [] purchases=purchase.split(",");
			//UUID主键
			pur.setPurchaseId(Tools.getRandomString(32));
			//采购数量赋值
			pur.setPurchaseNumber(Integer.valueOf(purchases[0]));
			//采购金额赋值
			pur.setPurchasePrice(BigDecimal.valueOf(Double.valueOf(purchases[1])));
			//供应商赋值
			pur.setSupplierId(purchases[2]);
			//采购说明赋值
			pur.setPurchaseExplain(purchases[3]);
			//公司ID赋值
			pur.setCompanyId("1");
			//审核状态赋值(未审核)
			pur.setAuditingStatus(0);
			//操作人赋值
			pur.setOper(users.getBuName());
			//采购时间赋值
			pur.setPurchaseTime(new Date());
			//是否有效(有效)
			pur.setIsva(1);
			//采购标识(材料采购1)
			pur.setPurchaseTab(1);
			//调用增加材料订单的方法
			rows=purchaseDao.insertSelective(pur);
			
			//拆分前台传来采购明细的数组
			String [] purchaseDateils=purchaseDateil.split("#");
			for (int i = 0; i < purchaseDateils.length; i++) {
				//创建材料采购订单明细类对象
				ListenPurchaseDetail pd=new ListenPurchaseDetail();
				//采购订单ID赋值
				pd.setPurchaseId(pur.getPurchaseId());
				//循环按,号分割后的数组
				for (int j = 0; j < purchaseDateils[i].split(",").length; j++) {
					//UUID主键
					pd.setPdId(Tools.getRandomString(32));
					//业务ID赋值
					pd.setVocationalWorkId(purchaseDateils[i].split(",")[0]);
					//订单材料名称
					pd.setPdName(purchaseDateils[i].split(",")[1]);
					//订单材料单价赋值
					pd.setPdPrice(BigDecimal.valueOf(Double.valueOf(purchaseDateils[i].split(",")[2])));
					//订单材料数量赋值
					pd.setPdNumber(Integer.valueOf(purchaseDateils[i].split(",")[3]));
					//订单材料小计赋值
					pd.setPdSubtotal(BigDecimal.valueOf(Double.valueOf(purchaseDateils[i].split(",")[4])));
					//是否有效(有效)
					pd.setIsva(1);
				}
			//调用增加材料采购订单明细的方法
			rows=purchaseDetailDao.insertSelective(pd);
			}
			
		}
			
		return rows;
	}
	
	/**
	 * @title 查询材料采购
	 * @param ListenPurchase
	 * @return List
	 * @author 江宇
	 * @dateTime 2018年12月10日
	 * @versions 1.0
	 */
	@Override
	public List<ListenPurchase> materialFindAll(Map<String, Object> map) {
		
		return purchaseDao.materialFindAll(map);
	}
	
	/**
	 * @title 查询材料采购行数
	 * @param ListenPurchase
	 * @return int
	 * @author 江宇
	 * @dateTime 2018年12月10日
	 * @versions 1.0
	 */
	@Override
	public int materialFindRows(Map<String, Object> map) {
		 
		return purchaseDao.materialFindRows(map);
	}
	
	/**
	 * @title 根据ID查询材料采购
	 * @param String
	 * @return ListenPurchase
	 * @author 江宇
	 * @dateTime 2018年12月10日
	 * @versions 1.0
	 */
	@Override
	public ListenPurchase selectPurchaseId(String purchaseId) {
		 
		return purchaseDao.selectByPrimaryKey(purchaseId);
	}
	
	/**
	 * @title 删除采购原材料和采购原材料明细 
	 * @param String
	 * @return int
	 * @author 江宇
	 * @dateTime 2018年12月11日
	 * @versions 1.0
	 */
	@Override
	public int deleteMateria(String purchaseId) {
		int rows=0;
		
		Map<String, Object> map=new HashMap<>();
		//采购订单ID赋值
		map.put("purchaseId", purchaseId);
		//调用查询采购订单明细的方法
		List<ListenPurchaseDetail> list=purchaseDetailDao.materialDetailFindAll(map);
		//循环采购订单明细List
		for (ListenPurchaseDetail  listenPurchaseDetail: list) {
			//创建采购订单明细对象
			ListenPurchaseDetail pd=new ListenPurchaseDetail();
			//采购订单明细ID赋值
			pd.setPdId(listenPurchaseDetail.getPdId());
			//是否有效(无效)
			pd.setIsva(2);
			//调用修改采购订单明细的方法
			rows=purchaseDetailDao.updateByPrimaryKeySelective(pd);
		}
		
		//创建采购订单类对象
		ListenPurchase pur=new ListenPurchase();
		//采购订单明细ID赋值
		pur.setPurchaseId(purchaseId);
		//是否有效(无效)
		pur.setIsva(2);
		//调用修改采购订单的方法
		purchaseDao.updateByPrimaryKeySelective(pur);
		
		
		return rows;
	}

	/**
	 * 采购乐器增加
	 * @param musicPurchase 采购字符串
	 * @param musicPurchaseDetail 采购详情字符串
	 * @param session 登录进来的用户信息
	 * @return int 类型
	 */
	@Override
	public int addMusicPurchase(String musicPurchase, String musicPurchaseDetail,HttpSession session) {
		//新建一个采购对象
		ListenPurchase listenPurchase=new ListenPurchase();
		//定义rows 默认为0
		int rows=0;
		//取登录用户的信息
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		//判断用户名是否为空
		if(!Tools.isEmpty(users.getBuName())) {
			//赋值给操作人
			listenPurchase.setOper(users.getBuName());
		}
		//判断用户站点是否为空
		if(!Tools.isEmpty(users.getSite())) {
			//赋值给公司id
			listenPurchase.setCompanyId(users.getSite());
		}
		//已“，”分割前台转过来的采购字符串
		String[] musicSplit = musicPurchase.split(",");
		//赋值采购主键id
		listenPurchase.setPurchaseId(Tools.getRandomString(32));
		//赋值采购金额
		listenPurchase.setPurchasePrice(BigDecimal.valueOf(Double.valueOf(musicSplit[0])));
		//赋值采购数量
		listenPurchase.setPurchaseNumber(Integer.valueOf(musicSplit[1]));
		//赋值采购说明
		listenPurchase.setPurchaseExplain(musicSplit[2]);
		//赋值采购有效
		listenPurchase.setIsva(1);
		//赋值采购状态
		listenPurchase.setAuditingStatus(0);
		//赋值采购供应商默认为1
		listenPurchase.setSupplierId("1");
		//赋值采购标识默认为2
		listenPurchase.setPurchaseTab(2);
		//赋值采购时间
		listenPurchase.setPurchaseTime(new Date());
		//新增采购顶单的方法
	 	int row=purchaseDao.insertSelective(listenPurchase);
	 	//判断新增采购顶单采购成功
	 	if(row>0) {
	 		//已“，”分割前台转过来的采购详情字符串
			String[] musicDetailSplit = musicPurchaseDetail.split(",");
			//遍历分割的内容
			for (int i = 0; i < musicDetailSplit.length; i++) {
				//新建一个订单详情对象
				ListenPurchaseDetail purchaseDetail=new ListenPurchaseDetail();
				//订单详情赋值采购id
				purchaseDetail.setPurchaseId(listenPurchase.getPurchaseId());
				//已“#”分割
				String[] splits = musicDetailSplit[i].split("#");
				//遍历
				for (int j = 0; j < splits.length; j++) {
					//赋值采购订单详情id
					purchaseDetail.setPdId(Tools.getRandomString(32));
					//赋值采购订单详情业务id
					purchaseDetail.setVocationalWorkId(splits[0]);
					//赋值采购订单详情采购名称
					purchaseDetail.setPdName(splits[1]);
					//赋值采购订单详情采购明细金额
					purchaseDetail.setPdPrice(BigDecimal.valueOf(Double.valueOf(splits[2])));
					//赋值采购订单详情采购数量
					purchaseDetail.setPdNumber(Integer.valueOf(splits[3]));
					//赋值采购订单详情采购小计
					purchaseDetail.setPdSubtotal(BigDecimal.valueOf(Double.valueOf(splits[4])));
					//赋值采购订单详情操作人
					purchaseDetail.setOper(users.getBuName());
					//赋值采购订单详情有效
					purchaseDetail.setIsva(1);
				}
				//新增采购订单详情的方法
				rows=purchaseDetailDao.insertSelective(purchaseDetail);
			}
	 	}
	 	//返回rows
		return rows;
	}

	/**
	 * 修改乐器
	 * @param musicPurchase 采购字符串
	 * @param musicPurchaseDetail 采购详情字符串
	 * @param purId 订单id
	 * @param session session
	 * @param updateType 修改状态
	 * @return int 类型
	 */
	@Override
	public int updateMusicPurchase(String musicPurchase, String musicPurchaseDetail, String purId,String updateType,HttpSession session) {
		//取登录用户的信息
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		//获取采购订单对象
		ListenPurchase listenPurchase=new ListenPurchase();
		//定义rows 默认为0
		int rows=0;
		//判断用户名是否为空
		if(!Tools.isEmpty(users.getBuName())) {
			//赋值给操作人
			listenPurchase.setOper(users.getBuName());
		}
		//判断用户站点是否为空
		if(!Tools.isEmpty(users.getSite())) {
			//赋值给公司id
			listenPurchase.setCompanyId(users.getSite());
		}
		//已“，”分割前台转过来的采购字符串
		String[] musicSplit = musicPurchase.split(",");
		//赋值采购主键id
		listenPurchase.setPurchaseId(purId);
		//赋值采购金额
		listenPurchase.setPurchasePrice(BigDecimal.valueOf(Double.valueOf(musicSplit[0])));
		//赋值采购数量
		listenPurchase.setPurchaseNumber(Integer.valueOf(musicSplit[1]));
		//赋值采购说明
		listenPurchase.setPurchaseExplain(musicSplit[2]);
		//赋值采购有效
		listenPurchase.setIsva(1);
		//赋值采购状态 默认为0
		listenPurchase.setAuditingStatus(0);
		//赋值采购供应商默认为1
		listenPurchase.setSupplierId("1");
		//赋值采购标识默认为2
		listenPurchase.setPurchaseTab(2);
		//赋值采购时间
		listenPurchase.setPurchaseTime(new Date());
		//修改采购订单的方法
	 	int rowks=purchaseDao.updateByPrimaryKeySelective(listenPurchase);
	 	//判断修改采购顶单成功
	 	if(rowks>0) {
	 		//判断修改状态为“2”
	 		if("2".equals(updateType)) {
	 			//已“，”分割前台转过来的采购详情字符串
				String[] musicDetailSplit = musicPurchaseDetail.split(",");
				//遍历
				for (int i = 0; i < musicDetailSplit.length; i++) {
					//新建一个订单详情对象
					ListenPurchaseDetail purchaseDetail=new ListenPurchaseDetail();
					//订单详情赋值采购id
					purchaseDetail.setPurchaseId(purId);
					//已“#”分割
					String[] splits = musicDetailSplit[i].split("#");
					//遍历
					for (int j = 0; j < splits.length; j++) {
						//赋值采购订单详情id
						purchaseDetail.setPdId(Tools.getRandomString(32));
						//赋值采购订单详情业务id
						purchaseDetail.setVocationalWorkId(splits[0]);
						//赋值采购订单详情采购名称
						purchaseDetail.setPdName(splits[1]);
						//赋值采购订单详情采购明细金额
						purchaseDetail.setPdPrice(BigDecimal.valueOf(Double.valueOf(splits[2])));
						//赋值采购订单详情采购数量
						purchaseDetail.setPdNumber(Integer.valueOf(splits[3]));
						//赋值采购订单详情采购小计
						purchaseDetail.setPdSubtotal(BigDecimal.valueOf(Double.valueOf(splits[4])));
						//赋值采购订单详情操作人
						purchaseDetail.setOper(users.getBuName());
						//赋值采购订单详情有效
						purchaseDetail.setIsva(1);
					}
					//调用增加采购订单详情的方法
					rows=purchaseDetailDao.insertSelective(purchaseDetail);
				}
			//判断修改状态为“1”
			}else if("1".equals(updateType)) {
					 //判断采购订单详情不为空
			 	 if(!"".equals(musicPurchaseDetail)) {
			 		//已“，”分割前台转过来的采购详情字符串
					 String[] musicDetailSplit = musicPurchaseDetail.split(",");
					 //遍历
					 for (int i = 0; i < musicDetailSplit.length; i++) {
						 //新建一个采购订单详情对象
						 ListenPurchaseDetail purchaseDetail=new ListenPurchaseDetail();
						//赋值采购订单详情id
						 purchaseDetail.setPurchaseId(purId);
						 String[] musicDetails = musicDetailSplit[i].split("#");
						 for (int j = 0; j < musicDetails.length; j++) {
							//赋值采购订单详情id
							 purchaseDetail.setPdId(musicDetails[0]);
							//赋值采购订单详情数量
							 purchaseDetail.setPdNumber(Integer.valueOf(musicDetails[1]));
							//赋值采购订单详情小计
							 purchaseDetail.setPdSubtotal(BigDecimal.valueOf(Double.valueOf(musicDetails[2])));
							//赋值采购订单详情操作人
							 purchaseDetail.setOper(users.getBuName());
						}
						//调用修改采购订单详情的方法
						rows=purchaseDetailDao.updateByPrimaryKeySelective(purchaseDetail);
					}
			 	 }
				 	
			}
	 	}
	 	//返回rows
	 	return rows;
	}
	/**
	 * 显示分店采购乐器审核
	 * @param purchase 采购对象
	 * @return 采购对象集合
	 */
	@Override
	public List<ListenPurchase> findMusicState(ListenPurchase purchase) {
		 //调用采购Dao显示分店采购乐器审核的方法
		 List<ListenPurchase> list = purchaseDao.findMusicState(purchase);
		 //遍历集合
		 for (ListenPurchase listenPurchase : list) {
			 //转换时间
			 listenPurchase.setPurchaseStringTime(Tools.getTimeStr(listenPurchase.getPurchaseTime()));
		}
		 //返回集合
		 return list;
	}
	/**
	 * 查询分店采购乐器审核页数
	 * @param purchase 采购对象
	 * @return int 行数
	 */
	@Override
	public int findMusicStateRows(ListenPurchase purchase) {
		//调用采购Dao显示分店采购乐器审核行数的方法
		return purchaseDao.findMusicStateRows(purchase);
	}
	/**
	 * 显示分店总经理采购乐器审核
	 * @param purchase 采购对象
	 * @return 采购对象集合
	 */
	@Override
	public List<ListenPurchase> findManagerState(ListenPurchase purchase) {
		 //调用采购Dao显示分店总经理采购乐器审核的方法
		List<ListenPurchase> list = purchaseDao.findManagerState(purchase);
		 //遍历集合
		 for (ListenPurchase listenPurchase : list) {
			 //转换时间
			 listenPurchase.setPurchaseStringTime(Tools.getTimeStr(listenPurchase.getPurchaseTime()));
		}
		//返回集合
		 return list;
	}
	/**
	 * 查询分店总经理采购乐器审核页数
	 * @param purchase 采购对象
	 * @return int 行数
	 */
	@Override
	public int findManagerStateRows(ListenPurchase purchase) {
		//调用采购Dao显示分店总经理审核行数的方法
		return purchaseDao.findManagerStateRows(purchase);
	}
	/**
	 * 通过采购订单id查询库存
	 * @param purchaseId 采购id
	 * @return 采购对象集合
	 */
	@Override
	public List<ListenPurchase> findByMusicalNumber(String purchaseId) {
		return purchaseDao.findByMusicalNumber(purchaseId);
	}
	/**
	 * 修改采购订单的状态
	 * @param purchase 采购对象
	 * @return int类型
	 */
	@Override
	public int updateState(ListenPurchase purchase) {
		return purchaseDao.updateState(purchase);
	}

	/**
	 * 增加到销售订单
	 * @param purchaseId 采购id
	 * @param session 登陆页面用户信息
	 * @return int
	 */
	@Override
	public int addSell(String purchaseId,HttpSession session) {
		//新建一个销售类
		ListenSell sells=new ListenSell();
		//给sort赋值给采购id
		sells.setSort(purchaseId);
		//通过销售对象查出销售id返回采购订单对象集合    
		List<ListenPurchase> listsSell = purchaseDao.findSellId(sells);
		//获取登录的用户信息
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		//定义一个rows 默认为0
		int rows=0;
		//通过采购id查询采购订单对象
		ListenPurchase purchase = purchaseDao.findPurchase(purchaseId);
		//新建一个销售对象
		ListenSell sell=new ListenSell();
		//赋值给采购id给标示
		sell.setSort(purchase.getPurchaseId());
		//赋值给销售状态
		sell.setSellState(1);
		//赋值给销售有效
		sell.setIsva(1);
		//赋值给销售时间
		sell.setSellTime(new Date());
		//赋值给销售公司 默认为1
		sell.setSellCompany("1");
		//赋值给销售对象
		sell.setSellTarget(users.getSite());
		//赋值给销售总数量
		sell.setSellNumber(purchase.getPurchaseNumber());
		//赋值给销售价格
		sell.setSellPrice(purchase.getPurchasePrice());
		//赋值给销售操作人
		sell.setOper(users.getBuName());
		//赋值给销售退货状态默认为1
		sell.setReturnState(1);
		//判断如果查询销售id出来的集合不为空
		if(!listsSell.isEmpty()) {
			//新建一个销售订单详情集合
			List<ListenSellDetail> list = new ArrayList<>();
			//遍历查出销售id集合采购订单详情集合
			for (ListenPurchase listenPurchase : listsSell) {
				//调用根据销售id查询销售详情对象集合方法放到销售订单详情集合
				list=sellDetailDao.findByDetails(listenPurchase.getSellId());
				//销售对象赋值给销售id
				sell.setSellId(listenPurchase.getSellId());
			}
			//修改销售订单的方法
			sellDao.updateByPrimaryKeySelective(sell);
			//新建一个销售订单详情对象
			ListenSellDetail sellDetail=new ListenSellDetail();
			//遍历根据销售id查询销售详情对象的集合
			for (ListenSellDetail listenSellDetail : list) {
				//取到销售详情对象id
				String sellId=listenSellDetail.getSdId();
				//调用删除销售详情对象的方法
				sellDetailDao.deleteByPrimaryKey(sellId);
			}
			//通过采购id查询出采购订单详情集合
			List<ListenPurchaseDetail> lists=purchaseDetailDao.findDetail(purchaseId);
			//赋值给销售订单详情销售id
			sellDetail.setSellId(sell.getSellId());
			//遍历通过采购id查询出采购订单详情集合
			for (ListenPurchaseDetail listenPurchaseDetail : lists) {
				//赋值给销售订单详情乐器id
				sellDetail.setMuId(listenPurchaseDetail.getVocationalWorkId());
				//赋值给销售订单详情主键id
				sellDetail.setSdId(Tools.getRandomString(32));
				//赋值给销售订单详情乐器数量
				sellDetail.setMuNumber(listenPurchaseDetail.getPdNumber());
				//赋值给销售订单详情乐器价格
				sellDetail.setMuPrice(listenPurchaseDetail.getPdPrice());
				//赋值给销售订单详情乐器小计
				sellDetail.setSubtotal(listenPurchaseDetail.getPdSubtotal());
				//赋值给销售订单详情乐器名称
				sellDetail.setMuName(listenPurchaseDetail.getMuName());
				//赋值给销售订单详情有效
				sellDetail.setIsva(1);
				//赋值给销售订单详情乐器类型名称
				sellDetail.setMtName(listenPurchaseDetail.getMtName());
				//赋值给销售订单详情操作时间
				sellDetail.setOpenTime(new Date());
				//赋值给销售订单详情操作人
				sellDetail.setOper(users.getBuName());
				//调用新增销售订单详情的方法
				rows =sellDetailDao.insertSelective(sellDetail);
			}
			}else {
				//赋值给销售id
				sell.setSellId(Tools.getRandomString(32));
				//新增销售订单的方法
				sellDao.insertSelective(sell);
				//通过采购id查询出采购订单详情集合
				List<ListenPurchaseDetail> list=purchaseDetailDao.findDetail(purchaseId);
				//新建一个销售订单详情对象
				ListenSellDetail sellDetail=new ListenSellDetail();
				//赋值给销售订单详情销售id
				sellDetail.setSellId(sell.getSellId());
				//遍历通过采购id查询出采购订单详情集合
				for (ListenPurchaseDetail listenPurchaseDetail : list) {
					//赋值给销售订单详情乐器id
					sellDetail.setMuId(listenPurchaseDetail.getVocationalWorkId());
					//赋值给销售订单详情主键id
					sellDetail.setSdId(Tools.getRandomString(32));
					//赋值给销售订单详情乐器数量
					sellDetail.setMuNumber(listenPurchaseDetail.getPdNumber());
					//赋值给销售订单详情乐器价格
					sellDetail.setMuPrice(listenPurchaseDetail.getPdPrice());
					//赋值给销售订单详情乐器小计
					sellDetail.setSubtotal(listenPurchaseDetail.getPdSubtotal());
					//赋值给销售订单详情乐器名称
					sellDetail.setMuName(listenPurchaseDetail.getMuName());
					//赋值给销售订单详情有效
					sellDetail.setIsva(1);
					//赋值给销售订单详情乐器类型名称
					sellDetail.setMtName(listenPurchaseDetail.getMtName());
					//赋值给销售订单详情操作时间
					sellDetail.setOpenTime(new Date());
					//赋值给销售订单详情操作人
					sellDetail.setOper(users.getBuName());
					//调用新增销售订单详情的方法
					rows =sellDetailDao.insertSelective(sellDetail);
				}
			}
			//返回rows
			return rows;
		}
		

	/**
	 * 增加到质检
	 * @param purchaserString 字符串
	 * @param session 登录用户的信息
	 * @return int
	 */
	@Override
	public int addQuality(String purchaserString,HttpSession session) {
		//新建一个质检类
		ListenQualityTesting testing=new ListenQualityTesting();
		//取到登录的用户信息
		ListenBackstageUser users=(ListenBackstageUser) session.getAttribute("user");
		//判断用户是否为空
		if(users!=null) {
			//判断用户名是否为空
			if(!Tools.isEmpty(users.getBuName())) {
				//赋值给操作人
				testing.setOper(users.getBuName());
			}
		}
		//赋值给质检id主键
		testing.setQtId(Tools.getRandomString(32));
		//赋值给质检对象业务id
		testing.setVocationalWorkId(purchaserString.split(",")[0]);
		//赋值给公司id
		testing.setCompanyId(purchaserString.split(",")[1]);
		//赋值给质检数量
		testing.setQtNumber(Integer.valueOf(purchaserString.split(",")[2]));
		//赋值有效
		testing.setIsva(1);
		//赋值给质检状态 默认为1
		testing.setQtStatus(1);
		//赋值给质检类型 默认为2
		testing.setQtType(2);
		//赋值给操作时间
		testing.setQtTime(new Date());
		//返回 int
		return testingDao.insertSelective(testing);
	}
	/**
	 * 查看分公司质检
	 * @param map map对象
	 * @return 质检对象集合
	 */
	@Override
	public List<ListenQualityTesting> findQualityAll(Map<String, Object> map) {
		//调用质检Dao查看分公司质检的方法
		return testingDao.findQualityTesting(map);
	}
	/**
	 * 查看分公司质检行数
	 * @param map map对象
	 * @return int 行数
	 */
	@Override
	public int findQualityRows(Map<String, Object> map) {
		//调用质检Dao查看分公司质检行数的方法
		return testingDao.findQualityTestingCount(map);
	}
	/**
	 * 入库
	 * @param qtId 质检id
	 * @param vocationalWorkId 业务id
	 * @param session 登录用户的信息
	 * @return 工具类
	 */
	@Override
	public int addStock(String qtId, String vocationalWorkId,HttpSession session) {
		//获取登录的用户信息
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		//定义rows
		int rows=0;
		//零售价盈利百分比
		BigDecimal retail = new BigDecimal("0.3");
		//批发价盈利百分比
		BigDecimal wholesale = new BigDecimal("0.5");
		//查询质检详情的map
		Map<String, Object> qtdMap=new HashMap<>();
		//给map赋值一个转过来的质检id
		qtdMap.put("qtId", qtId);
		//调用质检详情Dao通过查询质检详情的map查询出质检详情集合
		List<ListenQualityTestingDetail> qualityDetailList=qualityDetailDao.findQualityTestingDetail(qtdMap);
		//遍历质检详情集合
		for (ListenQualityTestingDetail listenQualityTestingDetail : qualityDetailList) {
			//查询库存的map
			Map<String, Object> stockMap=new HashMap<>();
			//给查询库存的map赋值乐器id
			stockMap.put("muId", listenQualityTestingDetail.getVocationalWorkId());
			//给查询库存的map赋值公司id
			stockMap.put("companyId", users.getSite());
			//订单详情的map
			Map<String, Object> detailMaps=new HashMap<>();
			//给查询订单详情的map赋值业务id
			detailMaps.put("vocationalWorkId", listenQualityTestingDetail.getVocationalWorkId());
			//给查询订单详情的map赋值采购id
			detailMaps.put("purchaseId", vocationalWorkId);
			//算出质检通过的数量
			int newNumber=listenQualityTestingDetail.getQtdNumber()-listenQualityTestingDetail.getQtdNoNumber();
			//调用采购详情Dao通过查询订单详情的map查询出订单详情集合
			List<ListenPurchaseDetail> findDetailList = purchaseDetailDao.findDetailByMap(detailMaps);
			//遍历订单详情集合
			for (ListenPurchaseDetail listenPurchaseDetail : findDetailList) {
				//新建一个库存对象
				ListenStock stocks=new ListenStock();
				//通过公司id和乐器id查询库存是否存在
				Map<String, Object> stockMap2=new HashMap<>();
				//给查询库存是否存在的map赋值公司id
				stockMap2.put("companyId", users.getSite());
				//给查询库存是否存在的map赋值乐器id
				stockMap2.put("muId", listenPurchaseDetail.getVocationalWorkId());
				//调用库存Dao查出库存对象
				ListenStock stock = stockDao.findByMuId(stockMap2);
				//判断库存是否存在
				if(stock!=null){
				    //存在则修改库存
					int joinNumber=stock.getStockNumber()+newNumber;
					//修改库存
					stocks.setStockId(stock.getStockId());
					//判断库存批发价是否为空
					if(stocks.getWholesale()==null) {
						//设置批发价
						stocks.setWholesale(listenPurchaseDetail.getPdPrice().add(listenPurchaseDetail.getPdPrice().multiply(wholesale)));
					}
					//判断库存零售价受否为空
					if(stocks.getRetail()==null) {
						//设置零售价
						stocks.setRetail(listenPurchaseDetail.getPdPrice().add(listenPurchaseDetail.getPdPrice().multiply(retail)));
					}
					//赋值库存操作时间
					stocks.setOpenTime(new Date());
					//赋值库存数量
					stocks.setStockNumber(joinNumber);
					//赋值库存操作人
					stocks.setOper(users.getBuName());
					//库存Dao修改的方法
					stockDao.updateByPrimaryKeySelective(stocks);
				//不存在则新增个库存	
				}else {
					/**
					 * 新增库存
					 */
					//赋值库存公司id
					stocks.setCompanyId(users.getSite());
					//赋值库存有效
					stocks.setIsva(1);
					//赋值库存乐器id
					stocks.setMuId(listenPurchaseDetail.getVocationalWorkId());
					//赋值库存id
					stocks.setStockId(Tools.getRandomString(32));
					//赋值库存数量
					stocks.setStockNumber(newNumber);
					//赋值库存零售价
					stocks.setRetail(listenPurchaseDetail.getPdPrice().add(listenPurchaseDetail.getPdPrice().multiply(retail)));//设置零售价
					//赋值库存批发价
					stocks.setWholesale(listenPurchaseDetail.getPdPrice().add(listenPurchaseDetail.getPdPrice().multiply(wholesale)));//设置批发价
					//赋值库存操作时间
					stocks.setOpenTime(new Date());
					//赋值库存操作人
					stocks.setOper(users.getBuName());
					//库存Dao增加的方法
					stockDao.insertSelective(stocks);
				}
				
				
			}
			
			//库存Dao查询库存的方法
			ListenStock stocksd = stockDao.findByMuId(stockMap);
			
			/**
			 * 新增库存记录
			 */
			//新建一个库存记录对象
			ListenStockRecord stockRecord=new ListenStockRecord();
			//赋值库存记录id
			stockRecord.setSrId(Tools.getRandomString(32));
			//赋值库存id
			stockRecord.setStockId(stocksd.getStockId());
			//赋值库存乐器名称
			stockRecord.setMuName(stocksd.getMuName());
			//赋值库存记录出入库时间
			stockRecord.setSrTime(new Date());
			//赋值库存记录出入库数量
			stockRecord.setSrNumber(newNumber);
			//赋值库存记录出入库说明
			stockRecord.setSrExplain("入库,分店采购");
			//赋值库存记录有效
			stockRecord.setIsva(1);
			//赋值库存记录操作时间
			stockRecord.setOpenTime(new Date());
			//赋值库存记录操作人
			stockRecord.setOper(users.getBuName());
			//调用库存记录Dao新增库存记录的方法
			stockRecordDao.insertSelective(stockRecord);
		}
		/**
		 * 修改财务
		 */
		//调用财务Dao通过公司id查询出公司财务
		ListenFinance companyFinance = financeDao.findByCompanyId(users.getSite());
		//新建一个公司财务对象
		ListenFinance companyFinance2=new ListenFinance();
		//调用采购Dao根据订单id查询订单对象
	    ListenPurchase findPurchase = purchaseDao.findPurchase(vocationalWorkId);
		//修改之后的财务
		BigDecimal ad = companyFinance.getCompanyPrice().subtract(findPurchase.getPurchasePrice());
		//赋值公司财务价格
		companyFinance2.setCompanyPrice(ad);
		//赋值公司财务操作时间
		companyFinance2.setOpenTime(new Date());
		//赋值公司财务主键id
		companyFinance2.setFinanceId(companyFinance.getFinanceId());
		//赋值公司财务操作人
		companyFinance2.setOper(users.getBuName());
		//修改公司财务的方法
		financeDao.updateByPrimaryKeySelective(companyFinance2);
		
		/**
		 * 修改质检状态
		 */
		//新建一个质检对象
		ListenQualityTesting  listenQualityTesting=new ListenQualityTesting();
		//设置质检状态默认为3
		listenQualityTesting.setQtStatus(3);
		//要修改的id
		listenQualityTesting.setQtId(qtId);
		//赋值操作人
		listenQualityTesting.setOper(users.getBuName());;
		//调用质检Dao修改质检状态的方法
		testingDao.updateByPrimaryKeySelective(listenQualityTesting);
		/**
		 * 增加财务明细
		 */
		//新建一个公司财务详情对象
		ListenFinanceDetail financeDetail=new ListenFinanceDetail();
		//赋值公司财务详情主键id
		financeDetail.setFdId(Tools.getRandomString(32));
		//赋值公司财务id
		financeDetail.setFinanceId(companyFinance.getFinanceId());
		//赋值公司财务详情操作时间
		financeDetail.setOpenTime(new Date());
		//赋值公司财务详情时间
		financeDetail.setFdTime(new Date());
		//赋值公司财务详情操作人
		financeDetail.setOper(users.getBuName());
		//赋值公司财务详情数量
		financeDetail.setFdNumber(findPurchase.getPurchaseNumber());
		//赋值公司财务详情业务id
		financeDetail.setVocationalWorkId(vocationalWorkId);
		//赋值公司财务详情金额
		financeDetail.setFdPrice(findPurchase.getPurchasePrice());
		//赋值公司财务详情有效
		financeDetail.setIsva(1);
		//赋值公司财务详情说明
		financeDetail.setSort("支出，分公司采购");
		//新增公司财务详情的方法
		rows=financeDetailDao.insertSelective(financeDetail);
		//返回rows
		return rows;
	}

	/**
	 * 通过公司id查询公司财务
	 * @param session 登录用户的个人区间
	 * @return 返回公司财务对象
	 */
	@Override
	public ListenFinance findFinanceByCompanyId(String site) {
		//调用财务Dao通过公司id查询出公司财务
		return financeDao.findByCompanyId(site);
	}
	/**
	 * 通过采购id查询销售id
	 * @param sell 销售对象
	 * @return 采购集合
	 */
	@Override
	public List<ListenPurchase> findSellId(ListenSell sell) {
		return purchaseDao.findSellId(sell);
	}

	
	
}
